package org.example.B3;
import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;

public class WorkOver {
    private StateMachine<State, Event> stateMachine;   // 状态机对象
    private GuardCondition guardCondition = new GuardCondition();
    public WorkOver() {
        init();
    }

    public void init() {
        guardCondition.setExtractedString(new StringBuilder());

        // 初始化状态机配置
        StateMachineConfig<State, Event> config = new StateMachineConfig<>();

        // 配置状态转换规则
        config.configure(State.ReadingChar)
                .permitIf(Event.quote, State.ReadString,guardCondition.when_firstQuote(),this::setQuoteCnt_eq_1)
                .ignore(Event.other);

        config.configure(State.ReadString)
                .permitIf(Event.quote, State.Output,guardCondition.when_secondQuote())
                .permitIf(Event.backslash, State.Transfering,guardCondition.when_backslash())
                .ignore(Event.other);

        config.configure(State.Transfering)
                .permit(Event.quote, State.ReadString)

                .permit(Event.other, State.ReadString);

        // 创建状态机实例
        stateMachine = new StateMachine<>(State.ReadingChar, config);
    }

    public void setCurrentChar(char currentChar){
        this.guardCondition.setCurrentChar(currentChar);
    }

    public void setQuoteCnt_eq_1() {
        guardCondition.setQuoteCnt(1);
    }

    public void fire(Event event){
        this.stateMachine.fire(event);
    }
    public State getCurrentState(){
        return this.stateMachine.getState();
    }
    public void printCurrentState() {
        System.out.println("当前状态: " + stateMachine.getState());
    }
}


